<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>权限管理</el-breadcrumb-item>
            <el-breadcrumb-item>角色列表</el-breadcrumb-item>
        </el-breadcrumb>

        <el-card>
            <el-row>
                <el-col>
                    <el-button type="primary">添加角色</el-button>
                </el-col>
            </el-row>
            <el-table :data="rolesList" border stripe>
                <el-table-column type="expand">
                    <template slot-scope="scope">
                        <el-row v-for="(itm1, i1) in scope.row.children" :key="i1" class="vcenter">
                            <el-col :span="5">
                                <el-tag>{{itm1.authName}}</el-tag>
                                <el-icon class="el-icon-caret-right"></el-icon>
                            </el-col>
                            <el-col :span="19">
                                <el-row v-for="(itm2, i2) in itm1.children" :key="i2" class="vcenter">
                                    <el-col :span="6">
                                        <el-tag type="success">{{itm2.authName}}</el-tag>
                                        <el-icon class="el-icon-caret-right"></el-icon>
                                    </el-col>
                                    <el-col :span="13">
                                        <el-tag closable type="warning" v-for="(itm3, i3) in itm2.children" :key="i3" @close="removeTag(scope.row,itm3.id)">
                                            {{itm3.authName}}
                                        </el-tag>
                                    </el-col>
                                </el-row>
                            </el-col>
                        </el-row>
                    </template>
                </el-table-column>
                <el-table-column type="index" label="序号"></el-table-column>
                <el-table-column label="角色名称" prop="roleName"></el-table-column>
                <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
                        <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
                        <el-button type="warning" icon="el-icon-setting" size="mini" @click="setRolesListDia(scope.row)">分配权限</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>

        <el-dialog
                title="提示"
                :visible.sync="setDialogVisible"
                width="50%"
                @close="deleteIdList"
                >
            <el-tree :data="setRolesList"
                     :props="defaultProps"
                     node-key="id"
                     default-expand-all
                     show-checkbox
                     :default-checked-keys="idList"
                     ref="treeRef"
            >
            </el-tree>
            <span slot="footer">
                <el-button @click="setDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="allotRights">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
    import { getRoles,deleteRoles,getRights,addRolesRights } from "../api/user";

    export default {
        name: "Roles",
        data() {
            return {
                roleId: '',
                idList: [],
                setDialogVisible: false,
                rolesList: [],
                setRolesList: [],
                tree: 'tree',
                defaultProps: {
                    label: 'authName',
                    children: 'children'
                }
            }
        },
        created() {
            this.getRolesList()
        },
        methods: {
            // 分配权限
            allotRights() {
                const keys = [
                    ...this.$refs.treeRef.getCheckedKeys(),
                    ...this.$refs.treeRef.getHalfCheckedKeys()
                ]
                const idStr = keys.join(',')
                addRolesRights(this.roleId,idStr).then(res => {
                    if (res.data.meta.status === 200) {
                        this.$message.success({
                            type: 'success',
                            message: '分配角色权限成功'
                        })
                        this.getRolesList()
                        this.setDialogVisible = false
                    }
                })

            },
            deleteIdList() {
                this.idList = []
            },
            // 获取权限
            async setRolesListDia(roles) {
                const res = await  getRights(this.tree)
                   if (res.data.meta.status === 200) {
                       this.setRolesList = res.data.data
                       this.getIdList(roles,this.idList)
                       this.setDialogVisible = true
                       this.roleId = roles.id
                       console.log(this.idList)
                }
            },
            // 递归获取三级权限id
            getIdList(node, arr) {
                if (!node.children) {
                    return arr.push(node.id)
                }
                node.children.forEach(item => this.getIdList(item,arr))
            },
            // 删除标签
            async removeTag(role,rightId) {
                const confirmResult = await this.$confirm('此操作将永久删除标签, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).catch(err => err)
               if (confirmResult === 'confirm') {
                   deleteRoles(role.id,rightId).then(res => {
                       if (res.data.meta.status === 200) {
                           this.$message.success({
                               type:"success",
                               message: '删除成功'
                           })
                           role.children = res.data.data
                       }
                   })
               } else {
                   this.$message.error('取消成功')
               }
            },
            getRolesList() {
                getRoles().then(res => {
                    if (res.data.meta.status === 200) {
                        this.rolesList = res.data.data
                    } else {
                        this.$message.error('获取角色失败')
                    }
                })
            }
        }
    }
</script>

<style scoped>
    .el-card {
        margin-top: 20px;
    }
    .el-tag {
        margin: 7px;
    }

    .vcenter {
        display: flex;
        align-items: center;
    }

</style>
